<?php
echo "<html>
		<head>
		<body>";
if ($handle = opendir('../data')) {

    while (false !== ($file = readdir($handle))) {
 
		$str = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ffxivcalc><abilities>\n"; 
		$lines = file("../data/".$file);
		foreach($lines as $line) {
			if (strpos($line,"ffxivcalc") == False) { 
				$fn = findString(NULL,NULL,"id=\"","\"",$line); 
				$med = findString(NULL, NULL, "<mediumIcon>", "</mediumIcon>",$line);
				$lar = findString(NULL, NULL, "<largeIcon>", "</largeIcon>",$line);
				echo "id: ".$fn."<br>";
				file_put_contents("../data/icons/large/".$fn.".png", file_get_contents($lar));
				file_put_contents("../data/icons/medium/".$fn.".png", file_get_contents($med));
				$pre = strpos ($line, "<mediumIcon>" ); 
				$str .= substr($line,0,$pre). "<mediumIcon>../data/icons/medium/".$fn.".png</mediumIcon><largeIcon>../data/icons/large/".$fn.".png</largeIcon></skill>\n";
			}
		}
		
		$str .= " </abilities></ffxivcalc>"; 
		$new_file = "../datan/".$file;
		if($file != '.' && $file != '..'){
			$fh = fopen($new_file, 'w'); 
			fwrite($fh,$str);
			fclose($fh); 
		}
		
 
    }
    closedir($handle);

}
echo "
	</body>
		</head></html>
		";

function findString($parentAnchor, $childAnchor, $startT, $endT, $file){
		$abort = false;
		$varDefStart = 1;
		if (!is_null($parentAnchor)){
			$varDefStart = strpos ($file, $parentAnchor);
			if ($varDefStart== False){
				$abort = true;
			}
		}
		if (!is_null($childAnchor)){
			$varDefStart = strpos ($file, $childAnchor, $varDefStart);
			if ($varDefStart== False){
				$abort = true;
			}
		}
		//if ($varDefStart >= 0 && $varDefStart<=strlen($file) && !($varDefStart==False)) {
			//echo "first anchor:".$varDefStart."<br>";
		$startPointRaw  = strpos  ($file, $startT, $varDefStart);
		if (($startPointRaw!= False) && ($abort == false)){
			$startPoint = $startPointRaw+strlen($startT);
			//echo "startpoint:".$startPoint."<br>";
			$endPoint = strpos  ($file, $endT, $startPoint);
			//echo "endpoint:".$endPoint."<br>";
			Return substr ($file, $startPoint, ($endPoint-($startPoint)));
		}else{
			Return "";
		}

}
/*$myFile = "all.xml";
	$fh = fopen($myFile, 'w');
	
	
	fwrite($fh, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<allSkills>\n".$outputString."</allSkills>\n");
	fclose($fh);*/
?>
